//
//  PickerCityView.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/13.
//

#import "PickerCityView.h"
#import "PickerCityTableCell.h"

#define kTabCell   @"kTabCell"

#define kTabCellHeight  50

@interface PickerCityView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UIView *wholeView,*bvv;

@property (nonatomic) UITableView *tableView;

@property (nonatomic) NSArray *arr;

@property (nonatomic) NSMutableArray *dataOneArr,*dataTwoArr,*dataThreeArr;

@property (nonatomic) UIButton *btn1,*btn2,*btn3;

@property (nonatomic) NSInteger btnTag;

@property (nonatomic) NSString *str1,*str2,*str3;

@property (nonatomic) UIImageView *tipIconImgView;

@property (nonatomic) UIView *lineV1,*lineV2,*lineV3;

@end

@implementation PickerCityView

- (instancetype)initWithFrame:(CGRect)frame withTitleStr:(NSString *)titleStr withDataArr:(NSArray *)arr{
    self = [super initWithFrame:frame];
    if (self) {
       
        self.backgroundColor = kBACK_SHADOWCOLOR;
        
        self.dataOneArr = [[NSMutableArray alloc] init];
        self.dataTwoArr = [[NSMutableArray alloc] init];
        self.dataThreeArr = [[NSMutableArray alloc] init];
        
        UIImage *img = [UIImage imageNamed:@"icon_idf13"];
        
        CGFloat kHeight = DimensionBaseIphone6(img.size.height) + 50 + 12 + 48 + (IS_IPhoneX_All ? kHeight_BottomSafe : 15);
        
        CGFloat tabH = arr.count * kTabCellHeight;
        
        if (arr.count >= 5) {
            tabH = kTabCellHeight *5;
        }
        
        kHeight += tabH;
        
        self.arr = arr;
        
        self.wholeView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kHeight)];
        self.wholeView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.wholeView];
        
        
        
        UIImageView *topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, DimensionBaseIphone6(img.size.height))];
        [self.wholeView addSubview:topImgView];
        topImgView.image = img;
        topImgView.userInteractionEnabled = YES;
        
        UILabel *titleLab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:16] color:[UIColor colorWithHexString:@"#022D06"]];
        [topImgView addSubview:titleLab];
        titleLab.textAlignment  = 1;
        titleLab.frame = CGRectMake(0, 0, kScreenWidth, topImgView.size.height);
        titleLab.text = esString(titleStr);
        
        UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 18 - 24, topImgView.bottom - 24, 24, 24)];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateNormal];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateSelected];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateHighlighted];
        [cancelBtn addTarget:self action:@selector(clickCacel) forControlEvents:UIControlEventTouchUpInside];
        [topImgView addSubview:cancelBtn];
        
        UIView *bvv = [[UIView alloc] initWithFrame:CGRectMake(0, topImgView.bottom, kScreenWidth, 51)];
        bvv.backgroundColor = [UIColor whiteColor];
        [self.wholeView addSubview:bvv];
        self.bvv = bvv;
        
        [self initBtn];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, bvv.bottom -1, kScreenWidth - 40, 1)];
        line.backgroundColor = kLineColor;
        [bvv addSubview:line];
        
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, bvv.bottom, kScreenWidth, self.wholeView.size.height - bvv.bottom)];
        backView.backgroundColor = [UIColor whiteColor];
        [self.wholeView addSubview:backView];
        
        UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, backView.frame.size.height - 48 - (IS_IPhoneX_All ? kHeight_BottomSafe : 15), kScreenWidth - 30, 48)];
        confirmBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:16];
        [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateNormal];
        [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateSelected];
        [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateHighlighted];
        [confirmBtn setTitle:@"Confirming" forState:UIControlStateNormal];
        [confirmBtn setTitle:@"Confirming" forState:UIControlStateSelected];
        [confirmBtn setTitle:@"Confirming" forState:UIControlStateHighlighted];
        [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateNormal];
        [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateSelected];
        [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateHighlighted];
        [confirmBtn addTarget:self action:@selector(clickConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:confirmBtn];

        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,kScreenWidth,tabH) style:UITableViewStylePlain];
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.showsVerticalScrollIndicator = NO;
        [backView addSubview:self.tableView];
        
        [self.tableView registerClass:[PickerCityTableCell class] forCellReuseIdentifier:kTabCell];
        
        self.dataOneArr = [PickerCityModel mj_objectArrayWithKeyValuesArray:arr];
        
        [self.tableView reloadData];

        
    }
    return self;
}

- (void)initBtn{
    self.btn1 = [[UIButton alloc] init];
    self.btn1.frame = CGRectMake(20, 0, (kScreenWidth - 40 - 40)/3, 50);
    [self.btn1 setTitle:@"Choose" forState:UIControlStateNormal];
    [self.btn1 setTitleColor:[UIColor colorWithHexString:@"#022D06"] forState:UIControlStateNormal];
    self.btn1.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    [self.bvv addSubview:self.btn1];
    self.btn1.contentHorizontalAlignment = 1;
    
    self.lineV1 = [[UIView alloc] initWithFrame:CGRectMake(self.btn1.left + 18, 46, 14, 4)];
    self.lineV1.backgroundColor = [UIColor colorWithHexString:@"#00BA60"];
    [self.bvv addSubview:self.lineV1];
    
    self.btn2 = [[UIButton alloc] init];
    self.btn2.frame = CGRectMake(self.btn1.right, 0, (kScreenWidth - 40 - 40)/3, 50);
    [self.btn2 setTitle:@"Choose" forState:UIControlStateNormal];
    [self.btn2 setTitleColor:[UIColor colorWithHexString:@"#022D06"] forState:UIControlStateNormal];
    self.btn2.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    [self.bvv addSubview:self.btn2];
    self.btn2.contentHorizontalAlignment = 1;
    
    self.lineV2 = [[UIView alloc] initWithFrame:CGRectMake(self.btn2.left + 18, 46, 14, 4)];
    self.lineV2.backgroundColor = [UIColor colorWithHexString:@"#00BA60"];
    [self.bvv addSubview:self.lineV2];
    
    self.btn3 = [[UIButton alloc] init];
    self.btn3.frame = CGRectMake(self.btn2.right, 0, (kScreenWidth - 40 - 40)/3, 50);
    [self.btn3 setTitle:@"Choose" forState:UIControlStateNormal];
    [self.btn3 setTitleColor:[UIColor colorWithHexString:@"#022D06"] forState:UIControlStateNormal];
    self.btn3.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    [self.bvv addSubview:self.btn3];
    self.btn3.contentHorizontalAlignment = 1;
    
    self.lineV3 = [[UIView alloc] initWithFrame:CGRectMake(self.btn3.left + 18, 46, 14, 4)];
    self.lineV3.backgroundColor = [UIColor colorWithHexString:@"#00BA60"];
    [self.bvv addSubview:self.lineV3];
    
    self.btn1.tag = 0;
    [self.btn1 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btn2.tag = 1;
    [self.btn2 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btn3.tag = 2;
    [self.btn3 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.lineV1.hidden = NO;
    self.lineV2.hidden = YES;
    self.lineV3.hidden = YES;
    
}

- (void)clickBtn:(UIButton *)btn{
    
    if ([btn.titleLabel.text isEqualToString:@"Choose"]) {
        return;
    }
    
    if (![btn.titleLabel.text isEqualToString:@"Choose"]) {
        
        if (btn.tag == 0) {
            
            self.str2 = @"";
            self.str3 = @"";
            
            self.lineV1.hidden = NO;
            self.lineV2.hidden = YES;
            self.lineV3.hidden = YES;
                    
            [self.tableView reloadData];
            
            [self.btn2 setTitle:@"Choose" forState:UIControlStateNormal];
            [self.btn2 setTitleColor:[UIColor colorWithHexString:@"#022D06"] forState:UIControlStateNormal];
            
            [self.btn3 setTitle:@"Choose" forState:UIControlStateNormal];
            [self.btn3 setTitleColor:[UIColor colorWithHexString:@"#022D06"] forState:UIControlStateNormal];
            
        }
        else if (btn.tag == 1){
            self.str3 = @"";
            
            self.lineV1.hidden = YES;
            self.lineV2.hidden = NO;
            self.lineV3.hidden = YES;
                    
            
            [self.tableView reloadData];
            
            [self.btn3 setTitle:@"Choose" forState:UIControlStateNormal];
            [self.btn3 setTitleColor:[UIColor colorWithHexString:@"#022D06"] forState:UIControlStateNormal];
            
        }
    }
    

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kTabCellHeight;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (!self.lineV1.hidden) {
        return self.dataOneArr.count;
    }
    else if (!self.lineV2.hidden) {
        return self.dataTwoArr.count;
    }

    return self.dataThreeArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PickerCityTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kTabCell];
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (!self.lineV1.hidden) {
        
        cell.model = self.dataOneArr[indexPath.row];
        
        if ([self.btn1.titleLabel.text isEqualToString:cell.model.cambridge]) {
            cell.titleLab.textColor = [UIColor colorWithHexString:@"#00BA60"];
        } else {
            cell.titleLab.textColor = [UIColor colorWithHexString:@"#022D06"];
        }
    }
    else if (!self.lineV2.hidden) {
        cell.model = self.dataTwoArr[indexPath.row];
        
        if ([self.btn2.titleLabel.text isEqualToString:cell.model.cambridge]) {
            cell.titleLab.textColor = [UIColor colorWithHexString:@"#00BA60"];
        } else {
            cell.titleLab.textColor = [UIColor colorWithHexString:@"#022D06"];
        }
    }
    else{
        
        cell.model = self.dataThreeArr[indexPath.row];
        
        if ([self.btn3.titleLabel.text isEqualToString:cell.model.cambridge]) {
            cell.titleLab.textColor = [UIColor colorWithHexString:@"#00BA60"];
        } else {
            cell.titleLab.textColor = [UIColor colorWithHexString:@"#022D06"];
        }
        
    }
    
    
    
//    if ([self.selIndexPath containsIndex:indexPath.row]) {
//        cell.titleLab.textColor = [UIColor colorWithHexString:@"#00BA60"];
//    }
//    else{
//        cell.titleLab.textColor = [UIColor colorWithHexString:@"#022D06"];
//    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PickerCityTableCell *selCell = [tableView cellForRowAtIndexPath:indexPath];
    PickerCityModel *mm  = selCell.model;
    
    NSArray *nextArr = mm.vanity;
    
    if (self.lineV1.hidden == NO) {
    
        self.str1 = esString(mm.cambridge);
        [self.btn1 setTitle:self.str1 forState:UIControlStateNormal];
        [self.btn1 setTitleColor:[UIColor colorWithHexString:@"#638473"] forState:UIControlStateNormal];

        self.lineV1.hidden = YES;
        self.lineV2.hidden = NO;
        self.lineV3.hidden = YES;
        
        self.dataTwoArr = [PickerCityModel mj_objectArrayWithKeyValuesArray:nextArr];
    
    }
  
    else  if (self.lineV2.hidden == NO){
        self.str2 = esString(mm.cambridge);
        [self.btn2 setTitle:self.str2 forState:UIControlStateNormal];
        [self.btn2 setTitleColor:[UIColor colorWithHexString:@"#638473"] forState:UIControlStateNormal];
        
        self.lineV1.hidden = YES;
        self.lineV2.hidden = YES;
        self.lineV3.hidden = NO;
        
        self.dataThreeArr = [PickerCityModel mj_objectArrayWithKeyValuesArray:nextArr];
        
    }
  
    else  if (self.lineV3.hidden == NO){
        self.str3 = esString(mm.cambridge);
        [self.btn3 setTitle:self.str3 forState:UIControlStateNormal];
        [self.btn3 setTitleColor:[UIColor colorWithHexString:@"#638473"] forState:UIControlStateNormal];
        self.lineV1.hidden = YES;
        self.lineV2.hidden = YES;
        self.lineV3.hidden = YES;
    }
    
    else{
        self.str3 = esString(mm.cambridge);
        [self.btn3 setTitle:self.str3 forState:UIControlStateNormal];
        [self.btn3 setTitleColor:[UIColor colorWithHexString:@"#638473"] forState:UIControlStateNormal];
        self.lineV1.hidden = YES;
        self.lineV2.hidden = YES;
        self.lineV3.hidden = YES;
    }
    

    [self.tableView reloadData];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]init];
    return vv;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]init];
    return vv;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001f;
}

- (void)clickConfirmBtn{
    
    if (PMStringIsEmpty(self.str1) || PMStringIsEmpty(self.str2) || PMStringIsEmpty(self.str3)) {
        [CreaditUtilsHelper boxShowWithMessage:@"Please Select Address"];
        return;
    }
    
    [self clickCacel];
    
    NSString *cityStr = [NSString stringWithFormat:@"%@-%@-%@",esString(self.str1),esString(self.str2),esString(self.str3)];
    if (_callbackCityStr) {
        _callbackCityStr(cityStr);
    }
    
}

- (void)showBottomView{
    UIImage *img = [UIImage imageNamed:@"icon_idf13"];
    CGFloat kHeight = DimensionBaseIphone6(img.size.height) +  50 + 12 + 48 + (IS_IPhoneX_All ? kHeight_BottomSafe : 15);
    CGFloat tabH = kTabCellHeight *5;
    kHeight += tabH;
    
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        
        weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight - kHeight, kScreenWidth, kHeight);
         
     } completion:^(BOOL finished) {}];
    
}

- (void)clickCacel{
    UIImage *img = [UIImage imageNamed:@"icon_idf13"];
    CGFloat kHeight = DimensionBaseIphone6(img.size.height) + 50 + 12 + 48 + (IS_IPhoneX_All ? kHeight_BottomSafe : 15);
    CGFloat tabH = kTabCellHeight *5;
    kHeight += tabH;
    
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        
         weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kHeight);
         
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
